package com.flong.modules.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.flong.modules.BaseController;
import com.flong.modules.model.oracle.Sequences;
import com.flong.modules.model.oracle.UserIndColumns;
import com.flong.modules.model.oracle.UserTables;
import com.flong.modules.model.oracle.Views;
import com.flong.modules.service.SequencesService;
import com.flong.modules.service.TableService;
import com.flong.modules.service.UserIndColumnsService;
import com.flong.modules.service.ViewService;
import com.flong.modules.utils.ObjectUtil;

@SuppressWarnings("all")
@WebServlet(name = "sqlScriptController", urlPatterns = "/sqlScriptController")
public class SqlScriptController extends BaseController {
	
	//----对象
	TableService tableService = new TableService();
	ViewService viewService = new ViewService();
	SequencesService sequencesService = new SequencesService();
	UserIndColumnsService userIndColumnsService = new UserIndColumnsService();
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = setCharacterEncoding(request, response);
		try {
			//获取请求执行方法
			String executeMethod = request.getParameter("executeMethod");
			//判断请求方法
			if("queryTableList".equals(executeMethod)){
				queryTableList(request, response);
			}else if("queryViewList".equals(executeMethod)){
				queryViewList(request, response);
			}else if("querySequenceList".equals(executeMethod)){
				querySequenceList(request, response);
			}else if("queryIndexList".equals(executeMethod)){
				queryIndexList(request, response);
			}else if("queryTableDetailList".equals(executeMethod)){
				queryTableDetailList(request, response);
			}else if("queryViewDetailList".equals(executeMethod)){
				queryViewDetailList(request, response);
			}
			
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



	/**
	 * @Description 查询所有视图
	 * @Author		liangjilong
	 * @Date		2017年3月10日 下午3:47:10
	 * @param request
	 * @param response
	 * @throws Exception
	 * @throws ServletException
	 * @throws IOException 		参数
	 * @return 		void 返回类型
	 */
	private void queryViewList(HttpServletRequest request, HttpServletResponse response) throws Exception, ServletException, IOException {
		List<Views> viewList = null;
		String viewName = request.getParameter("viewName");
		if(ObjectUtil.isEmpty(viewName)){
			viewList = viewService.getViews();
		}else{
			viewList = viewService.getViewByName(viewName);
			request.setAttribute("viewName", viewName);
		}
		request.setAttribute("viewList", viewList);
		request.getRequestDispatcher("/index1.jsp").forward(request, response);
		
	}
	
	/**
	 * @Description TODO
	 * @Author		liangjilong
	 * @Date		2017年3月10日 下午4:56:30
	 * @param request
	 * @param response
	 * @throws Exception
	 * @throws ServletException
	 * @throws IOException 		参数
	 * @return 		void 返回类型
	 */
	private void querySequenceList(HttpServletRequest request, HttpServletResponse response) throws Exception, ServletException, IOException {
		List<Sequences> sequencesList = null;
		String seqName = request.getParameter("sequenceName");
		if(ObjectUtil.isEmpty(seqName)){
			sequencesList = sequencesService.getSequences();
		}else{
			sequencesList = sequencesService.getSequencesByName(seqName);
			request.setAttribute("sequenceName", seqName);
		}
		request.setAttribute("sequencesList", sequencesList);
		request.getRequestDispatcher("/index2.jsp").forward(request, response);
	}
	/**
	 * @Description 查询所有索引信息
	 * @Author		liangjilong
	 * @Date		2017年3月10日 下午5:16:42
	 * @param request
	 * @param response 		参数
	 * @return 		void 返回类型
	 */
	private void queryIndexList(HttpServletRequest request, HttpServletResponse response) throws Exception, ServletException, IOException {
		
		List<UserIndColumns> indexList = null;
		String indexName = request.getParameter("indexName");
		if(ObjectUtil.isEmpty(indexName)){
			indexList = userIndColumnsService.getIndexs();
		}else{
			indexList = userIndColumnsService.getIndexListByName(indexName);
			request.setAttribute("indexName", indexName);
		}
		request.setAttribute("indexList", indexList);
		request.getRequestDispatcher("/index3.jsp").forward(request, response);
	}

	/**
	 * @Description 查询表的详细信息,信息有表的字段，表字段的描述，表的字段的长度等信息
	 * @Author		liangjilong
	 * @Date		2017年3月10日 下午3:47:59
	 * @param request
	 * @param response
	 * @throws Exception
	 * @throws ServletException
	 * @throws IOException 		参数
	 * @return 		void 返回类型
	 */
	private void queryTableDetailList(HttpServletRequest request, HttpServletResponse response) throws Exception, ServletException, IOException {
		
		 /*	List<UserTabColumns> userTabColumnList = TableColumnsService.getUserTabColumns(tableName);
		List<UserTabComments> userTabCommentList = UserTabCommentsService.getUserTabCommentsByName(tableName);
		List<UserColComments> userColCommentList = UserColCommentsService.getUserColCommentsByName(tableName);
		UserConsColumns userConsColumns = UserConsColumnsService.getUserConsColumnsByName(tableName);*/
		
	}
	
	/**
	 * @Description 查询视图的详细信息,信息有视图的字段，视图字段的描述，视图的字段的长度等信息
	 * @Author		liangjilong
	 * @Date		2017年3月10日 下午3:47:59
	 * @param request
	 * @param response
	 * @throws Exception
	 * @throws ServletException
	 * @throws IOException 		参数
	 * @return 		void 返回类型
	 */
	private void queryViewDetailList(HttpServletRequest request, HttpServletResponse response) throws Exception, ServletException, IOException {
		
		
	}
	/**
	 * @Description 查询表信息
	 * @Author		liangjilong
	 * @Date		2017年3月7日 下午5:26:17
	 * @param request
	 * @param response
	 * @throws Exception
	 * @throws ServletException
	 * @throws IOException 		参数
	 * @return 		void 返回类型
	 */
	private void queryTableList(HttpServletRequest request, HttpServletResponse response) throws Exception, ServletException, IOException {
		List<UserTables> tables = null;
		String tableName = request.getParameter("tableName");
		if(ObjectUtil.isEmpty(tableName)){
			 tables = tableService.getTables();
		}else{
			 tables = tableService.getTablesByName(tableName);
			 request.setAttribute("tableName", tableName);
		}
		request.setAttribute("tableList", tables);
		request.getRequestDispatcher("/index.jsp").forward(request, response);
	}
	

}
